package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Color;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ColorService;

/**
 * 类说明
 * 
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-5-14 上午11:35:46
 */
@Controller
public class ColorController {

	private final Logger log = Logger.getLogger(ColorController.class);

	@Resource(name = "colorService", type = ColorService.class)
	private ColorService colorService;

	@RequestMapping("/color/colors.do")
	public String colors(HttpServletRequest request, Pager pager) {
		if (pager == null) {
			pager = new Pager();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = colorService.getCount(params);
		pager.setTotalCount(totalCount);
		List<Color> colors = colorService.getAll(params, pager);
		request.setAttribute("colors", colors);
		request.setAttribute("pager", pager);
		return "color/colors";
	}

	@RequestMapping("/color/addPage.do")
	public String addPage() {
		return "color/add";
	}

	@RequestMapping(value="/color/add.do")
	public ModelAndView add(Color color) {
		color.setStatus(BaseInfoStatus.NORMAL);
		colorService.add(color);
		ModelAndView mav = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("color");
		resonse.setMessage("添加颜色记录成功");
		mav.addObject("response", resonse);
		return mav;
	}

	@RequestMapping("/color/delete.do")
	public ModelAndView delete(@RequestParam(value="id", required=true) long id) {
		Color color = colorService.getObject(id);
		color.setStatus(BaseInfoStatus.DELETED);
		colorService.modify(color);
		ModelAndView mav = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("color");
		resonse.setMessage("删除颜色记录成功");
		resonse.setCallbackType("");
		mav.addObject("response", resonse);
		return mav;
	}

	@RequestMapping("/color/getColor.do")
	public String getColor(HttpServletRequest request, long id) {
		Color color = colorService.getObject(id);
		request.setAttribute("color", color);
		return "color/modify";
	}
	
	@RequestMapping(value = "/color/get/{id}.do")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("color/detail");
		Color color = colorService.getObject(id);
		mv.addObject("color", color);
		return mv;
	}

	@RequestMapping("/color/modify.do")
	public ModelAndView modify(HttpServletRequest request, Color color) {
		colorService.modify(color);
		ModelAndView mav = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("color");
		resonse.setMessage("修改颜色记录成功");
		mav.addObject("response", resonse);
		return mav;
	}
	
	@RequestMapping("/color/lookuplist.do")
	public ModelAndView lookup(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = colorService.getCount(params);
		pager.setTotalCount(totalCount);
		List<Color> colors= colorService.getAll(params, pager);
		ModelAndView mv = new ModelAndView("color/color_lookup");
		mv.addObject("colors", colors);
		return mv;
	}
}
